package com.huazi.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huazi.commonutils.CommentResult;
import com.huazi.eduservice.client.VodClient;
import com.huazi.eduservice.entity.EduChapter;
import com.huazi.eduservice.entity.EduVideo;
import com.huazi.eduservice.service.EduChapterService;
import com.huazi.eduservice.service.EduVideoService;
import com.huazi.eduservice.vo.DeleteVodUtils;
import com.huazi.eduservice.vo.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-03-18
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/edu-chapter")
public class EduChapterController {
    @Autowired
    EduChapterService chapterService;

    @Autowired
    EduVideoService eduVideoService;

    @Resource
    VodClient vodClient;

    @GetMapping("getChapter/{id}")
    public CommentResult getChapter(@PathVariable String id){
        //根据课程id获取所有的章节
        List<Options> options = chapterService.getChapter(id);
        return CommentResult.getOK().data("chapters",options);
    }

    //添加章节信息
    @PostMapping("addChapter")
    public CommentResult addChapter(@RequestBody EduChapter eduChapter){
        boolean save = chapterService.saveOrUpdate(eduChapter);
        return CommentResult.getOK().data("msg",save);
    }
    //修改章节信息
    @PostMapping("updateChapter")
    public CommentResult updateChapter(@RequestBody EduChapter eduChapter){
        boolean save = chapterService.updateById(eduChapter);
        return CommentResult.getOK().data("msg",save);
    }
    //删除章节信息
    @GetMapping("deleteChapter/{id}")
    public CommentResult deleteChapter(@PathVariable String  id){
        //先删除小节视频
        List<EduVideo> videos = eduVideoService.list(new QueryWrapper<EduVideo>().eq("chapter_id", id));
        if(videos!=null&&videos.size()>0){
            String ids = DeleteVodUtils.getStringIds(videos);
//            System.out.println(ids);
            vodClient.deleteVodVideo(ids);
        }
        //删除章节前先删除小节
        eduVideoService.remove(new QueryWrapper<EduVideo>().eq("chapter_id",id));
        //在删除章节
        boolean save = chapterService.removeById(id);
        return CommentResult.getOK().data("msg",save);
    }
    //根据章节id查询章节
    @GetMapping("getOneChapter/{id}")
    public CommentResult getOneChapter(@PathVariable String  id){
        EduChapter title = chapterService.getOne(new QueryWrapper<EduChapter>().eq("id", id));
        return CommentResult.getOK().data("chapter",title);
    }
//    //根据章节id查询所有小节
    @GetMapping("getVideos/{id}")
    public CommentResult getVideos(@PathVariable String  id){
        List<EduVideo> title = eduVideoService.list(new QueryWrapper<EduVideo>().eq("chapter_id", id));
        return CommentResult.getOK().data("videos",title);
    }

}

